﻿Imports System.Data.SqlClient
Public Class frmProces
    Dim lstProcePlt As List(Of Integer)
    Dim idSelectedPlt As Integer = 0
    Public Sub remplirLstView()

        If cmbPlainte.SelectedIndex > 0 Then
            Partager.cmd = New SqlCommand("select idProc from procPlaint where idPlt = " & CType(cmbPlainte.SelectedItem, DictionaryEntry).Key, Partager.con)
            Partager.open()
            Partager.dr = Partager.cmd.ExecuteReader
            lstProcePlt = New List(Of Integer)()
            While Partager.dr.Read
                lstProcePlt.Add(Partager.dr.GetInt32(0))
            End While
        End If

        Partager.moi.remplirLstProce()

        List_tous.Items.Clear()

        For Each proce In Partager.moi.listProce
            'With proce
            '    .ref.remplir_lstRefAdv()
            '    .ref.remplir_lstRefClt()
            'End With

            If cmbPlainte.SelectedIndex > 0 Then


                Dim req As IEnumerable(Of Integer) = From idProc In lstProcePlt
                                                    Where idProc = proce.idProce

                If req(0) > 0 Then
                    List_tous.Items.Add(New ListViewItem(New String() {proce.idProce, proce.numProce, proce.code, proce.annee, proce.tribunal, _
                                                        proce.dateProce, proce.sujet, proce.src.nom}))
                End If
            Else
                List_tous.Items.Add(New ListViewItem(New String() {proce.idProce, proce.numProce, proce.code, proce.annee, proce.tribunal, _
                                                        proce.dateProce, proce.sujet, proce.src.nom}))
            End If
        Next
    End Sub


    Private Sub frmProces_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Try
            For i As Integer = 1990 To 2020
                Comb_annee.Items.Add(i)
            Next
            Partager.selectYear(Comb_annee)
            Partager.com_ville(Comb_mahkama, "Comb_mahkama", "mahkama")

            Partager.remplirClient(Comb_nom)

            Partager.moi.lstSource = New List(Of source)
            Partager.moi.remplirlstSource()
            Partager.remplircmbSource(cmbSource)

            Partager.moi.listProce = New List(Of Proce)
            'Partager.moi.remplirRef()

            'Partager.moi.remplir_List_client()
            'Partager.moi.remplir_List_enemi()
            cmbPlainte.Items.Add(New DictionaryEntry With {.Key = -1, .Value = "جميع المحاضر"})
            'remplir le combo des plaintes
            Partager.remplirCmbPlainte(cmbPlainte)
            cmbCaracAdv.SelectedIndex = 0
            cmbCaracClt.SelectedIndex = 1
        Catch ex As Exception
            MsgBox(ex.Message, MsgBoxStyle.Critical, "خطأ")
        End Try
    End Sub


    Private Sub btnAjout_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAjout.Click
        Try
            If (List_tous.SelectedIndices.Count > 0) Then
                MsgBox("عفوا لا يمكنك الإضافة، لإضافة محضر جديد عليك الضغط على الزر جديد ثم إدخال البيانات", MsgBoxStyle.Critical, "خطأ في الإضافة")
            Else
                If MsgBox("هل تريد فعلا إضافة هذا المحضر؟", MsgBoxStyle.YesNo Or MsgBoxStyle.Information, "إضافة محضر") = MsgBoxResult.Yes Then
                    If Not String.IsNullOrEmpty(txtCode.Text) And Not String.IsNullOrEmpty(txtNumProc.Text) And Not cmbSource.SelectedItem Is Nothing Then
                        If Me.cmbCaracClt.SelectedIndex <> Me.cmbCaracAdv.SelectedIndex Then
                            Dim trib As String = ""
                            If Comb_mahkama.Items.Count > 0 Then
                                trib = Comb_mahkama.SelectedItem.ToString()
                            End If
                            Dim procObjet As New Proce(CInt(txtNumProc.Text), CInt(txtCode.Text), CInt(Comb_annee.SelectedItem), _
                                                       trib, dteTmePicInscri.Value, _
                                                       rTxtSujet.Text, rTxtObserv.Text, cmbCaracClt.Text, cmbCaracAdv.Text)


                            Partager.deClt = DirectCast(cmbSource.SelectedItem, DictionaryEntry)
                            procObjet.src.id = CInt(Partager.deClt.Key)

                            Partager.deClt = DirectCast(Comb_adv.SelectedItem, DictionaryEntry)
                            procObjet.ref.Ref = CInt(Partager.deClt.Key)

                            Partager.moi.AjouterProce(procObjet)
                            procObjet.idProce = Partager.moi.listProce.Max(Function(p As Proce) p.idProce)


                            If cmbPlainte.SelectedIndex > 0 Then
                                'Ajouter la relation plainte proce si la plainte est choisi
                                Partager.deClt = DirectCast(cmbPlainte.SelectedItem, DictionaryEntry)
                                procObjet.ajouterRelPlt(Partager.deClt.Key)
                            End If
                            MsgBox("عملية الإضافة تمت بنجاح", MsgBoxStyle.Information, "محضر")
                            remplirLstView()
                            formVidange()
                        Else
                            MsgBox("لا يمكن إضافة نفس الصفة للطرفين", MsgBoxStyle.Critical, "محضر")
                        End If
                    Else
                        MsgBox("لم تدخل جميع المعلومات", MsgBoxStyle.Critical, "محضر")
                    End If
                End If
            End If
        Catch sqlEx As SqlException
            If sqlEx.Message.Contains("champsUnicite") Then
                MsgBox("عفوا، رقم هذا المحضر مستعمل من قبل", MsgBoxStyle.Critical, "خطأ")
            Else
                MsgBox(sqlEx.Message)
            End If
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
    End Sub

    Private Sub cmbNomClt_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Comb_nom.SelectedIndexChanged
        Try
            If Comb_nom.Text <> "" Then
                Partager.deClt = DirectCast(Comb_nom.SelectedItem, DictionaryEntry)
                Partager.remplir_CombAdvrT(CInt(Partager.deClt.Key), Comb_adv)
            End If
        Catch ex As Exception
            MsgBox(ex.Message, MsgBoxStyle.Critical, "خطأ")
        End Try
    End Sub

    Private Sub txtNumProc_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles txtNumProc.KeyPress
        Dim allowedChars As String = "1234567890"
        If e.KeyChar = ChrW(8) Then
            e.Handled = False
        ElseIf allowedChars.IndexOf(e.KeyChar) = -1 Then
            ' Invalid Character
            e.Handled = True
        End If
    End Sub

    Private Sub txtCode_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles txtCode.KeyPress
        Dim allowedChars As String = "1234567890"
        If e.KeyChar = ChrW(8) Then
            e.Handled = False
        ElseIf allowedChars.IndexOf(e.KeyChar) = -1 Then
            ' Invalid Character
            e.Handled = True
        End If
    End Sub

    Private Sub btnModif_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnModif.Click
        Try
            If List_tous.SelectedIndices.Count > 0 Then
                If MsgBox("هل تريد فعلا تعديل هذا المحضر؟", MsgBoxStyle.YesNo Or MsgBoxStyle.Information, "تعديل محضر") = MsgBoxResult.Yes Then
                    If Not String.IsNullOrEmpty(txtCode.Text) And Not String.IsNullOrEmpty(txtNumProc.Text) And Not cmbSource.SelectedItem Is Nothing Then
                        If Me.cmbCaracClt.SelectedIndex <> Me.cmbCaracAdv.SelectedIndex Then
                            Dim procObjet As New Proce(CInt(txtNumProc.Text), CInt(txtCode.Text), CInt(Comb_annee.SelectedItem), _
                                                       Comb_mahkama.SelectedItem.ToString(), dteTmePicInscri.Value, _
                                                       rTxtSujet.Text, rTxtObserv.Text, cmbCaracClt.Text, cmbCaracAdv.Text, , Partager.procSelected.idProce)



                            Partager.deClt = DirectCast(cmbSource.SelectedItem, DictionaryEntry)
                            procObjet.src.id = CInt(Partager.deClt.Key)

                            Partager.deClt = DirectCast(Comb_adv.SelectedItem, DictionaryEntry)
                            procObjet.ref.Ref = CInt(Partager.deClt.Key)

                            Partager.moi.modifierProce(procObjet)

                            If cmbPlainte.SelectedIndex > 0 Then
                                'Ajouter la relation plainte proce si la plainte est choisi
                                Partager.deClt = DirectCast(cmbPlainte.SelectedItem, DictionaryEntry)
                                procObjet.deleteRelPlt()
                                'Ajouter la relation plainte proce si la plainte est choisi
                                Partager.deClt = DirectCast(cmbPlainte.SelectedItem, DictionaryEntry)
                                procObjet.ajouterRelPlt(Partager.deClt.Key)
                            End If


                            MsgBox("عملية التعديل تمت بنجاح", MsgBoxStyle.Information, "محضر")
                            remplirLstView()
                            formVidange()

                            If idSelectedPlt > 0 And cmbPlainte.SelectedIndex = 0 Then
                                Comb_adv.Enabled = True
                                Comb_nom.Enabled = True
                                btnRechClt.Enabled = True
                                btnRechAdv.Enabled = True
                            End If
                        Else
                            MsgBox("لا يمكن إضافة نفس الصفة للطرفين", MsgBoxStyle.Critical, "محضر")
                        End If
                    Else
                        MsgBox("لم تدخل جميع المعلومات", MsgBoxStyle.Critical, "محضر")
                    End If
                End If
            Else
                MsgBox("اختر محضر من القائمة", MsgBoxStyle.Information, "خطأ في التعديل")
            End If
        Catch sqlEx As SqlException
            If sqlEx.Message.Contains("champsUnicite") Then
                MsgBox("عفوا، رقم هذا المحضر مستعمل من قبل", MsgBoxStyle.Critical, "خطأ")
            Else
                MsgBox(sqlEx.Message)
            End If
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
    End Sub

    ''' <summary>
    ''' Récupérer l'id plainte d'un proce
    ''' </summary>
    ''' <remarks>remark</remarks>
    Public Function pltProc(ByVal idProc As Integer) As Integer
        Partager.cmd = New SqlCommand("select idPlt from procPlaint where idProc = " & CInt(List_tous.SelectedItems(0).SubItems(0).Text), Partager.con)
        Partager.open()
        Dim idPlt As Integer = Partager.cmd.ExecuteScalar
        Return idPlt
    End Function
    Private Sub List_tous_DoubleClick(ByVal sender As Object, ByVal e As System.EventArgs) Handles List_tous.DoubleClick
        Try
            If List_tous.SelectedItems.Count > 0 Then
                Dim i As Integer = CInt(List_tous.SelectedItems(0).SubItems(0).Text)
                Dim req As IEnumerable(Of DictionaryEntry) = From it As DictionaryEntry In cmbPlainte.Items
                                                             Where it.Key = pltProc(i)

                If req.Count > 0 Then
                    MsgBox("هذا المحضر مرتبط بالشكاية رقم" & req(0).Value.ToString, MsgBoxStyle.Information, "شكاية")
                Else
                    MsgBox("هذا المحضر غير مرتبط بشكاية", MsgBoxStyle.Information, "شكاية")
                End If
            End If
        Catch ex As Exception
            MsgBox(ex.Message, MsgBoxStyle.Critical, "خطأ")
        End Try
       
    End Sub
    Private Sub List_tous_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles List_tous.SelectedIndexChanged
        Try
            If List_tous.SelectedItems.Count > 0 Then

                idSelectedPlt = CInt(List_tous.SelectedItems(0).SubItems(0).Text)
                Dim req As IEnumerable(Of Proce) = From pro In Partager.moi.listProce
                                                Where pro.idProce = idSelectedPlt

                Partager.procSelected = req(0)
                req(0).ref.remplir_lstRefAdv()
                req(0).ref.remplir_lstRefClt()
                remplireTxts(req(0))

                If pltProc(idSelectedPlt) > 0 Then
                    Comb_adv.Enabled = False
                    Comb_nom.Enabled = False
                    btnRechClt.Enabled = False
                    btnRechAdv.Enabled = False
                Else
                    Comb_adv.Enabled = True
                    Comb_nom.Enabled = True
                    btnRechClt.Enabled = True
                    btnRechAdv.Enabled = True
                End If
            Else
                If Not Partager.doubleVider Then
                    formVidange()
                End If
            End If
        Catch ex As Exception
            MsgBox(ex.Message, MsgBoxStyle.Critical, "خطأ")
        End Try
    End Sub

    Public Sub remplireTxts(ByVal objProce As Proce)
        txtCode.Text = objProce.code
        rTxtSujet.Text = objProce.sujet
        txtNumProc.Text = objProce.numProce
        rTxtObserv.Text = objProce.observation
        Comb_annee.SelectedItem = objProce.annee
        objProce.ref.remplir_lstRefAdv()
        objProce.ref.remplir_lstRefClt()
        Comb_nom.SelectedItem = New DictionaryEntry(objProce.ref.lstRefClt(0).id, objProce.ref.lstRefClt(0).nom)
        Comb_adv.SelectedItem = New DictionaryEntry(objProce.ref.lstRefAdv(0).id, objProce.ref.lstRefAdv(0).nom)
        cmbCaracAdv.SelectedItem = objProce.caracAdv
        cmbCaracClt.SelectedItem = objProce.caracClt
        dteTmePicInscri.Value = objProce.dateProce
        Comb_mahkama.SelectedItem = objProce.tribunal
        Dim selecteIndex As Integer = 0
        For Each ligne As DictionaryEntry In cmbSource.Items
            If ligne.Key = objProce.src.id Then
                cmbSource.SelectedIndex = selecteIndex
            End If
            selecteIndex += 1
        Next
        rTxtSujet.Text = objProce.sujet

    End Sub

    Private Sub btnNouv_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnNouv.Click
        Try
            formVidange()
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
    End Sub

    Public Sub formVidange()
        If cmbPlainte.SelectedIndex > 0 Then
            Partager.vider(Me, New ArrayList From {Comb_nom.Name, Comb_adv.Name, Comb_annee.Name, cmbPlainte.Name})
        Else
            Partager.vider(Me, New ArrayList From {Comb_annee.Name, cmbPlainte.Name})
        End If
        cmbCaracAdv.SelectedIndex = 0
        cmbCaracClt.SelectedIndex = 1
        Partager.selectYear(Comb_annee)
    End Sub

    Private Sub btnSupr_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSupr.Click
        Try
            If List_tous.SelectedIndices.Count > 0 Then
                If MsgBox("هل تريد فعلا حذف هذا المحضر ؟", MsgBoxStyle.YesNo Or MsgBoxStyle.Information, "حذف محضر") = MsgBoxResult.Yes Then
                    Partager.moi.supprimerProce(Partager.procSelected.idProce)
                    MsgBox("عملية الحذف تمت بنجاح", MsgBoxStyle.Information, "محضر")
                    remplirLstView()
                    formVidange()
                    'cas le proces a une plainte et cmbplainte.selectedindex = 0 
                    If idSelectedPlt > 0 And cmbPlainte.SelectedIndex = 0 Then
                        Comb_adv.Enabled = True
                        Comb_nom.Enabled = True
                        btnRechClt.Enabled = True
                        btnRechAdv.Enabled = True
                    End If
                End If
            Else
                MsgBox("اختر محضر من القائمة", MsgBoxStyle.Critical, "خطأ")
            End If
        Catch ex As Exception
            MsgBox(ex.Message, MsgBoxStyle.Critical, "خطأ")
        End Try
    End Sub

    Private Sub btnRech_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnRech.Click
        Me.Enabled = False
        frmRechProce.Show()
    End Sub

    Private Sub Btn_parc_mahkama_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Btn_parc_mahkama.Click
        frmAjoutSource.Show()
        Me.Enabled = False
    End Sub

    Private Sub btnRechPl_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnRechPl.Click
        Frm_Rech_Plainte.Show()
        Me.Enabled = True
        Partager.forPlainteRech = True
    End Sub

    Private Sub cmbPlainte_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmbPlainte.SelectedIndexChanged
        Try
        formVidange()
        remplirLstView()
        If cmbPlainte.SelectedIndex > 0 Then
            'Récupérer la référence de la plainte
            Partager.pltObje = New Plainte
            Partager.pltObje.Id = CType(cmbPlainte.SelectedItem, DictionaryEntry).Key
            Partager.pltObje.remplir_Plainte_Ref()
            Partager.refObj = New Reference
            Partager.refObj.Ref = Partager.pltObje.list_Plainte_Ref(0).ref

            Partager.refObj.remplir_lstRefAdv()
            Partager.refObj.remplir_lstRefClt()
            Comb_nom.SelectedItem = New DictionaryEntry(Partager.refObj.lstRefClt(0).id, Partager.refObj.lstRefClt(0).nom)
            Comb_adv.SelectedItem = New DictionaryEntry(Partager.refObj.lstRefAdv(0).id, Partager.refObj.lstRefAdv(0).nom)
            Comb_adv.Enabled = False
            Comb_nom.Enabled = False
            btnRechClt.Enabled = False
            btnRechAdv.Enabled = False
        Else
            Comb_adv.Enabled = True
            Comb_nom.Enabled = True
            btnRechClt.Enabled = True
            btnRechAdv.Enabled = True
        End If
        Catch ex As Exception
            MsgBox(ex.Message, MsgBoxStyle.Critical, "خطأ")
        End Try
    End Sub
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnFrmTribAjout.Click
        Me.Enabled = False
        Partager.viensDeProc = True
        Frm_Mahkama.Show()
    End Sub

    Private Sub btnRechClt_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnRechClt.Click
        Me.Enabled = False
        Partager.frmMoule = New Form
        Partager.frmMoule = Me
        Partager.type_rech_mowakil = "Frm_Ref"
        Frm_Rech_Clients.Show()
    End Sub

    Private Sub Button1_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnRechAdv.Click
        Me.Enabled = False
        Partager.frmMoule = New Form
        Partager.frmMoule = Me
        Partager.type_rech_adver = "Frm_Idafa_khossoum"
        Partager.moi.remplir_List_enemi()
        Frm_Rech_Adver.Show()
    End Sub

    Private Sub btnEnvoi_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnEnvoi.Click
        Try
            If List_tous.SelectedItems.Count > 0 Then
                Partager.viensDeProc = True
                Me.Enabled = False
                Frm_Envois.Show()
            Else
                MsgBox("اختر المحضر الذي تريد إرساله", MsgBoxStyle.Critical, "خطأ")
            End If
        Catch ex As Exception
            MsgBox(ex.Message, MsgBoxStyle.Critical, "خطأ")
        End Try
    End Sub
End Class